#ifndef _FREERTOS_HEAP_5_H_
#define _FREERTOS_HEAP_5_H_

#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/*----------------------------------------------------------------------------*/
#define portMAX_DELAY           (0xFFFFFFFFUL)

#define portPOINTER_SIZE_TYPE   uint32_t

#define portBYTE_ALIGNMENT      8

#if portBYTE_ALIGNMENT == 32
#define portBYTE_ALIGNMENT_MASK (0x001f)
#elif portBYTE_ALIGNMENT == 16
#define portBYTE_ALIGNMENT_MASK (0x000f)
#elif portBYTE_ALIGNMENT == 8
#define portBYTE_ALIGNMENT_MASK (0x0007)
#elif portBYTE_ALIGNMENT == 4
#define portBYTE_ALIGNMENT_MASK (0x0003)
#elif portBYTE_ALIGNMENT == 2
#define portBYTE_ALIGNMENT_MASK (0x0001)
#elif portBYTE_ALIGNMENT == 1
#define portBYTE_ALIGNMENT_MASK (0x0000)
#else /* if portBYTE_ALIGNMENT == 32 */
#error "Invalid portBYTE_ALIGNMENT definition"
#endif /* if portBYTE_ALIGNMENT == 32 */

/*----------------------------------------------------------------------------*/
#define configASSERT(x)
#define traceMALLOC(pvAddress, uiSize)
#define traceFREE(pvAddress, uiSize)

/*----------------------------------------------------------------------------*/
typedef long BaseType_t;

/* 由heap_5.c用来定义每个内存区域的起始地址和大小, 这些内存区域共同构成了FreeRTOS堆空间. */
typedef struct HeapRegion
{
    uint8_t * pucStartAddress;
    size_t xSizeInBytes;
} HeapRegion_t;

/* 用于从vPortGetHeapStats()中传递关于堆的信息. */
typedef struct xHeapStats
{
    /* 当前可用的堆总大小 - 这是所有空闲块的总和, 而不是可以分配的最大块. */
    size_t xAvailableHeapSpaceInBytes;
    /* 调用vPortGetHeapStats()时堆中所有空闲块的最大大小(以字节为单位). */
    size_t xSizeOfLargestFreeBlockInBytes;
    /* 调用vPortGetHeapStats()时堆中所有空闲块的最小大小(以字节为单位). */
    size_t xSizeOfSmallestFreeBlockInBytes;
    /* 调用vPortGetHeapStats()时堆中空闲内存块的数量. */
    size_t xNumberOfFreeBlocks;
    /* 自系统启动以来堆中可用内存的最小总量(所有可用块的总和). */
    size_t xMinimumEverFreeBytesRemaining;
    /* 调用pvPortMalloc()返回有效内存块的次数. */
    size_t xNumberOfSuccessfulAllocations;
    /* 成功释放内存块的vPortFree()调用次数. */
    size_t xNumberOfSuccessfulFrees;
} HeapStats_t;

/*----------------------------------------------------------------------------*/
/*
 * Used to define multiple heap regions for use by heap_5.c.  This function
 * must be called before any calls to pvPortMalloc() - not creating a task,
 * queue, semaphore, mutex, software timer, event group, etc. will result in
 * pvPortMalloc being called.
 *
 * pxHeapRegions passes in an array of HeapRegion_t structures - each of which
 * defines a region of memory that can be used as the heap.  The array is
 * terminated by a HeapRegions_t structure that has a size of 0.  The region
 * with the lowest start address must appear first in the array.
 */
void vPortDefineHeapRegions(const HeapRegion_t * const pxHeapRegions);

/*----------------------------------------------------------------------------*/
/*
 * Map to the memory management routines required for the port.
 */
void * pvPortMalloc(size_t xSize);
void vPortFree(void *pv);
void * pvPortCalloc(size_t xNum, size_t xSize);

size_t xPortGetFreeHeapSize(void);
size_t xPortGetMinimumEverFreeHeapSize(void);

void vPortGetHeapStats(HeapStats_t * pxHeapStats);
/*----------------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* _FREERTOS_HEAP_5_H_ */
